package com.qidian.buy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qidian.buy.common.pojo.BuyResult;
import com.qidian.buy.pojo.ItemCategory;
import com.qidian.buy.goods.mapper.ItemCategoryMapper;
import com.qidian.buy.goods.service.IItemCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2024-07-17 09:36:00
 */
@Service
public class ItemCategoryServiceImpl extends ServiceImpl<ItemCategoryMapper, ItemCategory> implements IItemCategoryService {

    @Autowired
    private ItemCategoryMapper categoryMapper;
    @Override
    public BuyResult queryAll() {
        return BuyResult.success(queryByParentId(0L));
    }

    private List<ItemCategory> queryByParentId(Long parentId){
        QueryWrapper<ItemCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<ItemCategory> cats = categoryMapper.selectList(wrapper);
        if(cats != null && cats.size() > 0){
            for (ItemCategory cat : cats){
                // 递归调用
                List<ItemCategory> children = queryByParentId(cat.getCatId());
                cat.setChildren(children);
            }
        }
        return cats;
    }
}
